import _ from 'lodash'

import { deepClone } from '@/utils/index'
import { Components } from '@/components/gene/config'
import { fillSettingConf,recycleSettingConf } from '@/components/gene/settingConfig'

import dayjs from 'dayjs'
/**
 * 表单json转换为后台需要的对象
 * @param item
 */
export function formItemConvertData(item, projectKey) {
  const data = {
    type: item.typeId,
    id: item.__config__.formId || null,
    label: item.__config__.label,
    defaultValue: item.__config__.defaultValue,
    isRequired: item.__config__.required,
    placeholder: item.placeholder,
    regList: item.__config__.regList,
    showLabel: item.__config__.showLabel,
    span: item.__config__.span,
    projectKey: projectKey
  }
  const extend = {}
  const param = dataParams[item.typeId]
  //若param有值,则遍历并添加到data的extend中
  if (param) {
    Object.keys(param).forEach(key => {
      const value = _.get(item, param[key])
      _.set(extend, key, value)
    })
    _.set(data, 'extend', extend)
  }
  return data
}

//类型关系map
const typeMap = new Map()

/**
 * 后台存储的数据转换为elementui表单需要的Json
 * @param data
 */
export function dbDataConvertForItemJson(data, serialNumber) {
  const { isRequired,placeholder } = data
  if (isRequired && !placeholder) {//必填项目验证未填默认提示语
    data.placeholder = '此题为必填项目'
  }
  if (!typeMap.size > 0) {
    //根据类型获取默认数据
    Components.forEach(item => {
      // console.log(item)
      typeMap.set(item.typeId, item)
    })
  }
  // 模板预览数据对接
  if(!data.sort) {
    // data.type = data.itemType
    data.sort = data.itemSort
    data.span = data.itemSpan
  }
  const defaultJsonItem = typeMap.get(data.type)
  const jsonItem = _.cloneDeep(defaultJsonItem)
  const param = dataParams[data.type]
  console.log(jsonItem)
  if (param) {
    Object.keys(param).forEach(key => {
      const value = _.get(data.extend, key)
      _.set(jsonItem, param[key], value)
    })
  }
  // console.log('convert--------', data)
  jsonItem.sort = data.sort
  jsonItem.typeId = data.type
  jsonItem.__config__.span =  data.span
  jsonItem.__config__.formId = data.id
  if (serialNumber) jsonItem.__config__.label = serialNumber + '--' + data.label
  else jsonItem.__config__.label = data.label
  jsonItem.__config__.required = data.isRequired
  jsonItem.__config__.regList = data.regList
  jsonItem.__config__.showLabel = data.showLabel
  if (data.defaultValue) {
    if (data.defaultValue.json) {
      jsonItem.__config__.defaultValue = JSON.parse(data.defaultValue.value)
    } else {
      jsonItem.__config__.defaultValue = data.defaultValue.value
    }
  }
  jsonItem.regList = data.regList
  jsonItem.placeholder = data.placeholder
  jsonItem.__vModel__ = 'field' + data.id

  return jsonItem
}

/**
 * 将表单设置组件中的数据转化为数据库需要的格式
 * @param settings
 * @param projectKey
 */
export function settingConvertData(settings,projectKey) {
  const collectionBeginTime = settings.beginAndEnd.collectionBeginTime
  const collectionEndTime = settings.beginAndEnd.collectionEndTime

  const data = {
    projectKey: projectKey,
    writeOnceTipsText: settings.showDefaultText.text,
    isEveryoneDayWriteOnce: settings.allowRepeatEnter.value,
    repeatEnterText: settings.allowRepeatEnter.text,
    collectionBeginTime: settings.beginAndEnd.collectionBeginTime,
    collectionEndTime: settings.beginAndEnd.collectionEndTime,
    notEnabledTipsText: settings.showEnabledTips.text,
    stoppedTipsText: settings.showStoppedTips.text,
    newWriteNotifyEmail: settings.messageInform.text,
    messageInform: settings.messageInform.value
  }

  return data
}

/**
 * 将表单设置组件中的数据转化为数据库需要的格式
 * @param data
 */
export function dbDataConvertForSetting(data) {
  const settings = deepClone(fillSettingConf)
  _.assign(settings,deepClone(recycleSettingConf))

  settings.allowRepeatEnter.value = data.isEveryoneDayWriteOnce
  if (settings.allowRepeatEnter.value) {
    settings.allowRepeatEnter.text = data.repeatEnterText
  } else {
    settings.allowRepeatEnter.text = fillSettingConf.allowRepeatEnter.text
  }
  if (data.writeOnceTipsText !== fillSettingConf.showDefaultText.text) {
    settings.showDefaultText.value = true
    settings.showDefaultText.text = data.writeOnceTipsText
  }
  if (data.collectionBeginTime || data.collectionEndTime) {
    settings.beginAndEnd.value = true
    settings.beginAndEnd.collectionBeginTime = data.collectionBeginTime
    settings.beginAndEnd.collectionEndTime = data.collectionEndTime
  }
  if (data.notEnabledTipsText !== recycleSettingConf.showEnabledTips.text) {
    settings.showEnabledTips.value = true
    settings.showEnabledTips.text = data.notEnabledTipsText
  }
  if (data.stoppedTipsText !== recycleSettingConf.showStoppedTips.text) {
    settings.showStoppedTips.value = true
    settings.showStoppedTips.text = data.stoppedTipsText
  }
  settings.messageInform.value = data.messageInform
  if (settings.messageInform.value) {
    settings.messageInform.text = data.newWriteNotifyEmail
  }
  console.log('-=-=-=-=',settings)
  return settings
}

/**
 * 不同属性的存在json的位置不用 使用变量记录key 通过lodash表达式获取 1 2 3 4 为typeId
 * @type {{'1': {maxlength: string, prepend: string, append: string}}}
 */

const dataParams = {
  //单行文本
  1: {
    prepend: '__slot__.prepend',
    maxlength: 'maxlength',
    append: '__slot__.append'
  },
  //计数器
  2: {
    min: 'min',
    max: 'max',
    maxlength: 'maxlength',
    step: 'step',
    'step-strictly': 'step-strictly',
    precision: 'precision',
    'controls-position': 'controls-position'
  },
  //下拉选择
  3: {
    options: '__slot__.options',
    filterable: 'filterable',
    multiple: 'multiple'
  },

  //单选框组
  5: {
    options: '__slot__.options',
    filterable: 'filterable',
    multiple: 'props.props.multiple'
  },
  //多选框组
  6: {
    optionType: '__config__.optionType',
    size: 'size',
    options: '__slot__.options',
    max: 'max',
    min: 'min'
  }, //开关
  7: {},
 //时间选择
  8: {},
  9: {
    max: 'max'
  }

}








